package com.cgbl.cg.ui.home

import android.graphics.Typeface
import android.os.Bundle
import android.util.TypedValue
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.viewpager.widget.ViewPager
import com.alibaba.android.arouter.facade.annotation.Route
import com.cgbl.cg.BR
import com.cgbl.cg.R
import com.cgbl.cg.adapter.*
import com.cgbl.cg.databinding.AppActivityRecommendCourseBinding
import com.cgbl.cg.ui.course.CourseMenuFragment
import com.cgbl.cg.vm.home.RecommendMasterCourseViewModel
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.model.CourseAllTypeBean
import com.robot.base.model.ModuleBean
import com.robot.base.util.DisplayHelper
import com.robot.base.util.arouter.ARouterUtil

@Route(path = AppConstants.Router.HOME.A_RECOMMEND_MASTER_COURSE)
class RecommendMasterCourseActivity :
    GLBaseActivity<AppActivityRecommendCourseBinding, RecommendMasterCourseViewModel>() {
    private var typeTab = 0

    private var mTitles = mutableListOf<String>()

    private var fragmentList = mutableListOf<Fragment>()

    private lateinit var moduleAdapter: ModuleAdapter

    var type: Int = 0
    override fun initContentView(savedInstanceState: Bundle?): Int =
        R.layout.app_activity_recommend_course

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        when (type) {
            AppConstants.BundleValue.MASTER_COURSE -> {
                addSingleTitleBar(getString(R.string.app_en_tips_39))
            }
            AppConstants.BundleValue.CLASS_CASE -> {
                addSingleTitleBar(getString(R.string.app_en_tips_40))
            }
            AppConstants.BundleValue.POPULAR_RESOURCES -> {
                addSingleTitleBar(getString(R.string.app_en_tips_41))
            }
            AppConstants.BundleValue.VIP -> {
                addSingleTitleBar(getString(R.string.app_en_tips_42))
            }
        }
        viewModel.type.set(type)
        initAdapter()
        viewModel.getModule(
            "$type"
        )

    }

    override fun initParam() {
        super.initParam()
        type = intent.getIntExtra(
            AppConstants.BundleKey.RECOMMEND_TYPE,
            AppConstants.BundleValue.MASTER_COURSE
        )
    }

    override fun initData() {
        super.initData()
        viewModel.initData()
        viewModel.getTab()
    }

    private fun initAdapter() {
        moduleAdapter = ModuleAdapter()
        binding.recyclerViewImg.apply {
            adapter = moduleAdapter
        }
        moduleAdapter.setOnItemClickListener { adapter, view, position ->
            when (type) {
                AppConstants.BundleValue.MASTER_COURSE -> {
                    ARouterUtil.goToActivityWithBundle(
                        AppConstants.Router.Mine.A_MINE_COURSE_PROFILE,
                        Bundle().apply {
                            putString(AppConstants.BundleKey.ID, moduleAdapter.data[position].id)
                        })
                }

                AppConstants.BundleValue.CLASS_CASE -> {

                }

                AppConstants.BundleValue.POPULAR_RESOURCES -> {
                    ARouterUtil.goToActivityWithBundle(
                        AppConstants.Router.Mine.A_MINE_RESOURCE_PROFILE,
                        Bundle().apply {
                            putString(AppConstants.BundleKey.ID, moduleAdapter.data[position].id)
                        })
                }

                AppConstants.BundleValue.VIP -> {
                    if (viewModel.tabType.get() == RecommendMasterCourseViewModel.TAB_COURSE) {
                        ARouterUtil.goToActivityWithBundle(
                            AppConstants.Router.Mine.A_MINE_COURSE_PROFILE,
                            Bundle().apply {
                                putString(
                                    AppConstants.BundleKey.ID,
                                    moduleAdapter.data[position].id
                                )
                            })
                    } else {
                        ARouterUtil.goToActivityWithBundle(
                            AppConstants.Router.Mine.A_MINE_RESOURCE_PROFILE,
                            Bundle().apply {
                                putString(
                                    AppConstants.BundleKey.ID,
                                    moduleAdapter.data[position].id
                                )
                            })
                    }
                }
            }
        }
    }

    override fun initViewObservable() {
        super.initViewObservable()

        viewModel.uc.titleDataEvent.observe(this) {
            moduleAdapter.setNewInstance(it as MutableList<ModuleBean>?)
        }

        viewModel.uc.tabDataType.observe(this) {
            it ?: return@observe
            resetFragment(it)
        }
    }

    private fun setTab() {
        binding.mViewPager2.adapter = object : FragmentStatePagerAdapter(supportFragmentManager) {
            override fun getItem(position: Int): Fragment = fragmentList[position]
            override fun getCount(): Int = fragmentList.size
        }

//        binding.tabLayout.setViewPager(binding.mViewPager2, mTitles.toTypedArray())
        binding.mViewPager2.setOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(
                position: Int, positionOffset: Float, positionOffsetPixels: Int
            ) {

            }

            override fun onPageSelected(position: Int) {
                typeTab = position
                setTabSize(position)
            }

        })
    }

    //select tab textsize
    private fun setTabSize(position: Int) {
        for (i in mTitles.indices) {
            if (binding.tabLayout.getTitleView(i) != null) {
                if (position == i) {
                    binding.tabLayout.getTitleView(i).setTextSize(
                        TypedValue.COMPLEX_UNIT_PX, DisplayHelper.dpToPx(16).toFloat()
                    )
                    binding.tabLayout.getTitleView(i).typeface = Typeface.DEFAULT_BOLD
                } else {
                    binding.tabLayout.getTitleView(i).setTextSize(
                        TypedValue.COMPLEX_UNIT_PX, DisplayHelper.dpToPx(14).toFloat()
                    )
                    binding.tabLayout.getTitleView(i).typeface = Typeface.DEFAULT
                }

            }
        }
    }

    private fun resetFragment(data: List<CourseAllTypeBean>) {
        mTitles.clear()
        fragmentList.clear()
        typeTab = 0
        mTitles.add("全部")
        fragmentList.add(
            RecommendMasterCourseFragment.newInstance(
                "0",
                viewModel.type.get() ?: AppConstants.BundleValue.MASTER_COURSE,
                viewModel.tabType.get() ?: RecommendMasterCourseViewModel.TAB_COURSE
            )
        )
        for (type in data) {
            mTitles.add(type.name ?: "")
            fragmentList.add(
                RecommendMasterCourseFragment.newInstance(
                    type.id + "",
                    viewModel.type.get() ?: AppConstants.BundleValue.MASTER_COURSE,
                    viewModel.tabType.get() ?: RecommendMasterCourseViewModel.TAB_COURSE
                )
            )
        }
        setTab()
        binding.mViewPager2.offscreenPageLimit = fragmentList.size
        binding.tabLayout.setViewPager(binding.mViewPager2, mTitles.toTypedArray())
        binding.tabLayout.currentTab = typeTab
        setTabSize(typeTab)
    }
}